package com.example.newsapp.ui.components.otherComponent.searchResultComponent

import android.content.Context
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.example.newsapp.data.database.NewsItems
import com.example.newsapp.data.`object`.PageNavigation
import com.google.gson.Gson

// 搜索后的结果
@Composable
fun ResultList(
    item: NewsItems,
    context: Context,
    gson: Gson,
    navController: NavController,
    focusRequester: FocusRequester,
    modifier: Modifier = Modifier
) {
    var pictureId = 0
    val picture = item.picture
    var keyboardController = LocalSoftwareKeyboardController.current
    // 将获取到的picture值转换为对应的id
    if (picture != "") {
        pictureId = context.resources.getIdentifier(
            picture.substringAfter("R.drawable."),
            "drawable",
            context.packageName
        )
    }
    val itemJson = gson.toJson(item)
    ListItem(
        headlineContent = {
            Text(
                text = item.title,
                style = MaterialTheme.typography.titleMedium
            )
        },
        supportingContent = {
            Text(
                text = item.source,
                color = Color(0xE15B5A5A)
            )
        },
        trailingContent = {
            if (pictureId != 0) {
                Image(
                    painter = painterResource(pictureId),
                    contentDescription = null,
                    modifier.size(120.dp, 100.dp)
                )
            }
        },
        modifier = Modifier
            .pointerInput(Unit) {
                focusRequester.freeFocus()
                keyboardController?.hide()
            }
            .focusRequester(focusRequester)
            .clickable {
                focusRequester.freeFocus()
                keyboardController?.hide()
                navController.navigate(
                    route = "${PageNavigation.newsDetailsScreen}?newsItemJson=${itemJson}"
                )
            },
        colors = ListItemDefaults.colors(containerColor = MaterialTheme.colorScheme.surfaceContainerLow)
    )
    HorizontalDivider(
        modifier = Modifier.padding(horizontal = 15.dp)
    )
}